package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    //查询
    @GetMapping("/page")
    public PageResult<BrandDTO> page(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "rowsPerPage", defaultValue = "10") int rowsPerPage,
            @RequestParam(value = "descending", defaultValue = "false") Boolean descending,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "key", required = false) String key

    ) {
        PageResult<BrandDTO> pageResult = brandService.page(page, rowsPerPage, descending, sortBy, key);
        return pageResult;
    }

    //保存
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {
        brandService.saveBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //删除
    @GetMapping("/deleteById")
    public ResponseEntity<Void> deleteById(@RequestParam("id") Long id) {
        brandService.deleteById(id);
        return ResponseEntity.status(200).build();
    }

    //修改,前端是put请求所以这样写
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {
        brandService.updateBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(brandService.queryByCategoryId(id));
    }

    //根据id查询品牌
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(brandService.queryById(id));
    }

    //根据Ids查询品牌信息
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(brandService.queryByIds(ids));
    }
}
